#!/usr/bin/env python

import sys, getopt, os, json

import utility


def calculate_user_behavior(user_behavior_folder):
    user_behavior_files = os.listdir(user_behavior_folder)
    user_behavior = {}
    for behavior_file in user_behavior_files:
        with open (user_behavior_folder+behavior_file, "r") as f:
            for line in f:
                period, times = map(int, line.split())
                if period not in user_behavior:
                    user_behavior[period] = 0
                user_behavior[period] += times

    target_file = "verificated_user_behavior"
    with open(target_file, "w") as f:
            time_periods = user_behavior.keys()
            time_periods.sort()
            for period in time_periods:
                line = str(period)+" "+str(user_behavior[period])+"\n"
                f.write(line)


def main(argv):
    try:
        opts, args = getopt.getopt(argv[1:], "d:")
    except getopt.GetoptError as err:
        print(err)
        sys.exit(2)


    for o, s in opts:
        if o == "-d":
            calculate_user_behavior(s)
    

if __name__ == "__main__":
    main(sys.argv)